# -*- coding: utf-8 -*-

# Copyright(c) 2011 OpenDomo Services SL. Licensed under GPL v3 or later
# Isidro Catalan <icatalan@opendomo.com>


"""
Models specific to the Opendomo plugin.
"""

import os

from elisa.core import common
from elisa.core.components.model import Model
from elisa.core.utils import defer, caching

from elisa.plugins.base.models.image import ImageModel
from elisa.plugins.opendomo import opendomo_api


class OpendomoResponseModel(Model):

    """
    A generic model containing a representation of the XML response returned by
    the Opendomo API.
    """

    pass


class LoginModel(Model):
    def __init__(self):
        super(LoginModel, self).__init__()
        self.success = False
        self.auth_token = None
        self.username = None
        self.password = None
        
class ItemModel(Model):
    def __init__(self):
        super(ItemModel, self).__init__()
        self.label = None
        self.href = None

class CommandModel(Model):
    def __init__(self):
        super(CommandModel, self).__init__()
        self.id = None
        self.label = None
        self.url = None
        self.state = None
        
class OpendomoPhotoModel(Model):
   def __init__(self):
        super(OpendomoPhotoModel, self).__init__()
        self.label = None
        self.href = None

class FileModel(Model):
    """
    It is an abstraction of a file from the filesystem.

    For each file we store the filename and complete path, as well as its size
    used later on to be displayed in the list (see ExampleFileViewMode below).
    Is also has a special method that is used when attempting to play the
    file.
    """

    def __init__(self):
        Model.__init__(self)
        self.name = None
        self.path = None
        self.size = None

    def get_playable_model(self):
        """
        This method is called by Moovida every time the user tries to play this
        model. It is expected to return a PlayableModel object (encapsulated
        in a deferred) containing the URI to the actual file to be played.

        Deferreds are Twisted specific objects for which information can be
        found at:
         - http://twistedmatrix.com/projects/core/documentation/howto/async.html
         - http://twistedmatrix.com/projects/core/documentation/howto/defer.html
        """
        # If the file was just a regular URI (such as http://example.com/f.mp4)
        # then we could have just passed it as-is to the constructor of MediaUri.
        uri = MediaUri({"scheme":"file", "path":self.path})
        playable_model = PlayableModel()
        playable_model.title = self.name
        playable_model.uri = uri
        dfr = defer.succeed(playable_model)
        return dfr
